//
//  AlterInformationViewController.m
//  Advertisement
//
//  Created by mac on 16/3/10.
//  Copyright © 2016年 Appcoda. All rights reserved.
//

#import "AlterInformationViewController.h"
#import "AFNetworking.h"

@interface AlterInformationViewController ()
@end

@implementation AlterInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"个人资料";
    
    _headLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 64, 100, kScreenH*0.16)];
    _headLabel.text = @"头像";
    [self.view addSubview:_headLabel];

    [self.view addSubview:self.headImageView];
    UITapGestureRecognizer *selectPhoto = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPhoto:)];
    [_headImageView addGestureRecognizer:selectPhoto];

    
    _personInfoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kScreenH*0.16+65, kScreenW, kScreenH - (kScreenH*0.16+64))];
//    _personInfoTableView.backgroundColor = [UIColor redColor];
    _personInfoTableView.delegate = self;
    _personInfoTableView.dataSource = self;
    _personInfoTableView.scrollEnabled = NO; //使tableview不可滑动
    [self.view addSubview:_personInfoTableView];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"Me" style:UIBarButtonItemStylePlain target:self action:@selector(backSuper:)];
    self.navigationItem.leftBarButtonItem = leftItem;
//    [self SureControlsPosition];
}


#pragma mark -------TableView的代理方法-------
//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 2;
    }
    return 3;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.detailTextLabel.textColor = [UIColor grayColor];
        switch (section) {
            case 0:
                if (row == 0) {
                    cell.textLabel.text = @"昵称";
                    cell.detailTextLabel.text = @"jhfdvhik";
                    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
                }else{
                    _userdefults1 = [NSUserDefaults standardUserDefaults];
                    NSString *acconutNumberString = [NSString stringWithFormat:@"%@",[_userdefults1 valueForKey:@"logname"]];
                    cell.textLabel.text = @"账号";
                    cell.detailTextLabel.text =[NSString stringWithFormat:@"%@",acconutNumberString];
                }
                break;
            case 1:
                if(row == 0){
                    cell.textLabel.text = @"性别";
                    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
                    button.frame = CGRectMake(100, 0, cell.frame.size.width-100, cell.frame.size.height);
                    
                }if(row == 1){
                    cell.textLabel.text = @"地区";
                }
                if (row == 2) {
                    cell.textLabel.text = @"个性签名";
                }
                break;
            default:
                break;
        }
    }
    return cell;
}
//调整 section之间的间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 15;
    
}
//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return kScreenH*0.08;
}

#pragma mark -----返回上一级进行代理传值-------
- (void)backSuper:(UIBarButtonItem *)sender {
    
    if (self.delegated && [self.delegated respondsToSelector:@selector(passValue:)]) {
        
        [self.delegated passValue:self.headImageView.image];
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -----轻拍手势的回调及选取照片-------
- (void)tapPhoto:(UITapGestureRecognizer *)sender {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册中选择", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    _picker = [[UIImagePickerController alloc]init];
    _picker.delegate = self;
    _picker.allowsEditing = YES;
    
    switch (buttonIndex) {
        case 0:
            NSLog(@"--1--------");
            break;
        case 1:
            NSLog(@"--2--------");
            _picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self presentViewController:_picker animated:YES completion:nil];
            break;
        case 2:
            NSLog(@"--3--------");
            _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:_picker animated:YES completion:nil];
            break;
        default:
            break;
        }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo
{    NSLog(@"=-=-=-=-=-=-%@",image);
    [self dismissViewControllerAnimated:YES completion:nil];

    _headImageView.image = image;
//    AFHTTPRequestOperationManager *manager =[[AFHTTPRequestOperationManager alloc]init];
//    manager.responseSerializer =[AFHTTPResponseSerializer serializer];
//    NSDictionary *parameters = @{@"image":_headImageView.image};
//    [manager POST:@"http://47.88.76.185:8080/Test/app_login" parameters:parameters//19.232
//          success:^(AFHTTPRequestOperation *operation, id responseObject)
//     {
//         NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//         NSString *rcode = [dic objectForKey:@"rcode"];
//         int a = [rcode intValue];
//         NSLog(@"----%@",parameters);
//         if (!a)
//         {
//             //                         [self.window showHUDWithText:rcode Type:ShowPhotoNo Enabled:YES];
//             NSLog(@"图像已经存在");
//         }
//     } failure:nil];
//     

   [_headImageView setBackgroundColor:[UIColor redColor]];
}


//打开相机hitHeadButton
- (void)hitPhotograpBt:(UIButton *)sender{
    // UIImagePickerControllerCameraDeviceRear 后置摄像头
    // UIImagePickerControllerCameraDeviceFront 前置摄像头
    NSLog(@"打开相册");
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!isCamera) {
        NSLog(@"没有摄像头");
        return ;
    }
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    // 编辑模式
    imagePicker.allowsEditing = YES;
    [self  presentViewController:imagePicker animated:YES completion:^{
    }];
}


//- (void)SureControlsPosition{
//    [_headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(50, 30));
//        make.top.equalTo(_headButton.mas_top).offset((kScreenH* 0.16-30)*0.5);
//        make.bottom.equalTo(_headButton.mas_bottom).offset(-(kScreenH* 0.16-30)*0.5);
//
//        make.left.mas_equalTo(15);
//    }];
//    

    
//    [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make
//    }];
    
//[_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//    make.size.mas_equalTo(CGSizeMake(90, 90));
//    make.left.mas_equalTo(kScreenW*0.5-45);
//    make.right.mas_equalTo(-(kScreenW*0.5-45));
//    make.top.mas_equalTo(84);
//    
//    
//}];
//    [_photographButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(90);
//        make.height.mas_equalTo(30);
//        make.top.equalTo(_headImageView.mas_bottom).offset(20);
//       
//    }];
//    [_selectPictureButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(_photographButton);
//        make.left.equalTo(_photographButton.mas_right).offset(40);
//        make.top.equalTo(_headImageView.mas_bottom).offset(20);
//        make.right.mas_equalTo(-90);
//    }];
//


//}


#pragma mark --------laying-----------
- (UIImageView *)headImageView {
    
    if (!_headImageView) {
        
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-kScreenH*0.16+5, 74, kScreenH*0.16-20, kScreenH*0.16-20)];
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = _headImageView.frame.size.width/2;
        _headImageView.userInteractionEnabled = YES;
        _headImageView.backgroundColor = [UIColor yellowColor];

    }
    return _headImageView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
